﻿
/*=========================================
* Author: Administrator
* DateTime:2017/6/21 12:43:57
* Description:$safeprojectname$
==========================================*/

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections.Generic;
using System.Collections;

namespace OrderSystem
{
    public class CookView : MonoBehaviour
    {
        
        public UnityAction<CookItem> ServerFood = null;
        private ObjectPool<CookItemView> objectPool = null;
        private List<CookItemView> cookItemsViewList = new List<CookItemView>();
        private Transform parent = null;
        
        private void Awake()
        {
            parent = this.transform.Find("Content");
            var prefab = Resources.Load<GameObject>("Prefabs/UI/CookItem");
            objectPool = new ObjectPool<CookItemView>(prefab, "CookPool");
            
        }

        
        public void UpdateCook(IList<CookItem> cooksData)
        {
            if (cooksData == null)
            {                           
                return;
            }
            else
            {
            }
            for (int i = 0; i < this.cookItemsViewList.Count; i++)
                objectPool.Push(this.cookItemsViewList[i]);

           this.cookItemsViewList.AddRange(objectPool.Pop(cooksData.Count));

            for (int i = 0; i < this.cookItemsViewList.Count; i++)
            {
   
                 if(i< cooksData.Count)
                 {
                     this.cookItemsViewList[i].transform.SetParent(parent);
                
                     this.cookItemsViewList[i].gameObject.SetActive(true);
                
                     var item = cooksData[i];
                     this.cookItemsViewList[i].transform.Find("Id").GetComponent<Text>().text = item.ToString();
                     Color color = Color.white;
                     if (item.state.Equals(E_CookState.Idle))
                     {
                
                         color = Color.green;
                     }
                     else if (item.state.Equals(E_CookState.MakeFood))
                     {
                         color = Color.yellow;
                         //食物做完了，上菜
                         StartCoroutine(MakeFood(5, item));
                     }
                 
                     this.cookItemsViewList[i].GetComponent<Image>().color = color;
                 }
            }
        }
        public void UpdateState(CookItem cookData)
        {
            if (cookData != null)
            {
                CookItem item = cookData;
                Debug.LogError(cookData.ToString() + cookData.state + "状态");
                this.cookItemsViewList[item.id].transform.SetParent(parent);

                this.cookItemsViewList[item.id].gameObject.SetActive(true);
             
                    this.cookItemsViewList[item.id].transform.Find("Id").GetComponent<Text>().text = item.ToString();
                    Color color = Color.white;
                    if (item.state.Equals(E_CookState.Idle))
                    {
                        color = Color.green;                    
                        this.cookItemsViewList[item.id].GetComponent<Image>().color = color;
                       
                    }
                    else if (item.state.Equals(E_CookState.MakeFood))
                    {
                        color = Color.yellow;                      
                        //食物做完了，上菜
                        StartCoroutine(MakeFood(5, item));
                        this.cookItemsViewList[item.id].GetComponent<Image>().color = color;

               }               
            }
                
        }

            private IEnumerator MakeFood(float time, CookItem item)
        {
            yield return new WaitForSeconds(time);
            Debug.Log(item.name + "开始做菜");
            ServerFood(item);
            Debug.Log(item.name+"菜做完了");     
            
        }
    }
}